package com.thz.system.service.blog.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.thz.system.mapper.blog.BlogMapper;
import com.thz.system.mapper.report.ReportMapper;
import com.thz.system.pojo.blog.Blog;
import com.thz.system.pojo.blog.dto.AddBlogDTO;
import com.thz.system.pojo.blog.dto.UpdateBlogDTO;
import com.thz.system.pojo.blog.vo.BlogDetailsVO;
import com.thz.system.pojo.report.Report;
import com.thz.system.service.blog.IBlogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.annotation.Repeatable;
import java.util.List;

/**
 * @Description 博客服务实现类
 * @Author thz
 * @Date 2025/5/10
 */
@Service
public class BlogServiceImpl implements IBlogService {

    @Autowired
    private BlogMapper blogMapper;

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public List<Blog> list(Long courseId) {
        return blogMapper.selectList(new LambdaQueryWrapper<Blog>()
                .eq(Blog::getCourseId, courseId));
    }

    @Override
    public int addBlog(AddBlogDTO addBlogDTO) {
        // 参数校验
        if (addBlogDTO == null) {
            throw new IllegalArgumentException("博客信息不能为空");
        }
        Blog blog = new Blog();
        BeanUtils.copyProperties(addBlogDTO, blog);
        // 调用Mapper插入数据
        return blogMapper.insert(blog);
    }

    @Override
    public int updateBlog(UpdateBlogDTO updateBlogDTO) {
        // 参数校验
        if (updateBlogDTO == null || updateBlogDTO.getBlogId() == null) {
            throw new IllegalArgumentException("博客信息或ID不能为空");
        }
        // 检查博客是否存在
        Blog blog = blogMapper.selectById(updateBlogDTO.getBlogId());
        if (blog == null) {
            throw new IllegalArgumentException("博客不存在");
        }

        Blog blogs = new Blog();
        BeanUtils.copyProperties(updateBlogDTO, blogs);
        blogs.setBlogId(blog.getBlogId());

        // 调用Mapper更新数据
        return blogMapper.updateById(blogs);
    }

    @Override
    public int deleteBlog(Long blogId) {
        // 参数校验
        if (blogId == null) {
            throw new IllegalArgumentException("博客ID不能为空");
        }
        
        // 检查博客是否存在
        Blog blog = blogMapper.selectById(blogId);
        if (blog == null) {
            throw new IllegalArgumentException("博客不存在");
        }

        int ret = blogMapper.deleteById(blogId);

        if (ret > 0) {
            deleteUserBlog(blogId);
        }
        // 调用Mapper删除数据
        return ret;
    }

    private void deleteUserBlog(Long blogId){
//        List<Report> reportList = reportMapper.selectList(new LambdaQueryWrapper<Report>()
//                .select(Report::getReportId)
//                .eq(Report::getBlogId, blogId));

//        for (Report report : reportList) {
//            reportMapper.deleteById(report.getReportId());
//        }
        reportMapper.delete(new LambdaQueryWrapper<Report>()
                .eq(Report::getBlogId, blogId));

    }

    @Override
    public BlogDetailsVO getBlogDetail(Long blogId) {
        // 参数校验
        if (blogId == null) {
            throw new IllegalArgumentException("博客ID不能为空");
        }
        
        // 调用Mapper查询数据
        Blog blog = blogMapper.selectById(blogId);
        if (blog == null) {
            throw new IllegalArgumentException("博客不存在");
        }

        BlogDetailsVO blogDetailsVO = new BlogDetailsVO();
        BeanUtils.copyProperties(blog, blogDetailsVO);
        
        return blogDetailsVO;
    }
}
